import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lcapp/api/interface/home_api.dart';
import 'package:lcapp/api/request/response_data.dart';
import 'package:lcapp/data/lc_video_data_entity.dart';
import 'package:lcapp/widget/asr_cache_image.dart';
import 'package:lcapp/widget/lc_gesture_detector.dart';

class VideoItem extends StatefulWidget {
  final LcVideoDataEntity entity;
  final Function cliclFun;
  const VideoItem({Key? key, required this.entity, required this.cliclFun}) : super(key: key);

  @override
  _VideoItemState createState() => _VideoItemState();
}

class _VideoItemState extends State<VideoItem> {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: (){
        if(widget.cliclFun!=null){
          widget.cliclFun();
        }
      },
      child: Container(
        color: Color.fromRGBO(0, 0, 0, 0.1),
        child: Stack(
          children: [
            Positioned(
              child: ASRCacheImage(
                image: widget.entity.coverPath!,
                fit: BoxFit.cover,
              ),
              left: 0,right: 0,top: 0,bottom: 0,
            ),
            Positioned(child: Container(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Visibility(child: Container(
                    margin: EdgeInsets.only(bottom: 20.w),
                    child: Text('#${widget.entity.goodsName}',style: TextStyle(
                        color: Colors.white,
                        fontSize: 24.sp
                    ),),
                  ),visible: widget.entity.goodsName!=null&&widget.entity.goodsName!.isNotEmpty,),
                  Row(
                    children: [
                      ClipRRect(
                        child: ASRCacheImage(
                          image: widget.entity.img!,
                          width: 60.w,
                          height: 60.w,
                          fit: BoxFit.fitWidth,
                        ),
                        borderRadius: BorderRadius.circular(40.w),
                      ),
                      SizedBox(width: 10.w,),
                      Expanded(child: Container(
                        child: Text(widget.entity.userName??'',style: TextStyle(
                            color: Colors.white,
                            fontSize: 26.sp
                        ),),
                      )),
                      SizedBox(width: 10.w,),
                      lcGestureDetector(
                        isLogin: true,
                        onTap: ()async{
                          ResponseData res = await HomeAPI.likeVideo(widget.entity.id!,widget.entity.userId!);
                          if(res.code==200){
                            if(widget.entity.likeState==1){
                              setState(() {
                                widget.entity.likeState = 0;
                                widget.entity.likeCounts = widget.entity.likeCounts!-1;
                              });
                            }else{
                              setState(() {
                                widget.entity.likeState=1;
                                widget.entity.likeCounts = widget.entity.likeCounts!+1;
                              });
                            }
                          }
                        },
                        child: Container(
                          child:
                          Image.asset(widget.entity.likeState==1?"assets/home/xinxing_red.png":"assets/home/xinxing.png",height: 40.w,),
                        )
                      ),
                      SizedBox(width: 5.w,),
                      Container(
                        child: Text('${widget.entity.likeCounts}',style: TextStyle(
                            color: Colors.white,
                            fontSize: 26.sp
                        ),),
                      ),
                    ],
                  )
                ],
              ),
            ),bottom: 10.w,left: 10.w,right: 10.w,)
          ],
        ),
      ),
    );
  }
}
